//
//  Status.swift
//  SinaWeibo
//
//  Created by 张洪毓 on 15/11/14.
//  Copyright © 2015年 张洪毓. All rights reserved.
//

import UIKit

class Status: NSObject {

    var created_at: String?
    var id: Int64 = 0
    var text: String?
    var source: String?
    var user: User?
    var retweeted_status: Status?
    //判断来源
    var weiboSource: String?{
        if retweeted_status != nil {
            return retweeted_status?.source
        }
        return source
    }
    var pic_urls: [[String: String]]?
    var imageURLs: [NSURL]? {
            guard let urls = pic_urls else {
                return nil
            }
            var tempArr = [NSURL]()
            for item in urls {
                let url = NSURL(string: item["thumbnail_pic"]!)
                tempArr.append(url!)
            }
            return tempArr
    }
    var pictureURLs: [NSURL]? {
        //如果转发微博不为空，返回转发微博的图片
            if retweeted_status != nil {
                return retweeted_status?.imageURLs
            }
        //如果转发微博为空，返回原创微博的图片
            return imageURLs
    }
    
    
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            if let dict = value as? [String: AnyObject] {
                user = User(dict: dict)
            }
            return
        }
        if key == "retweeted_status" {
            if let dict = value as? [String: AnyObject] {
                retweeted_status = Status(dict: dict)
            }
            return
        }
        
        super.setValue(value, forKey: key)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) { }

}
